package com.xugou.controller;

import com.xugou.pojo.Emp;
import com.xugou.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/emp")
public class EmpController {

    @Autowired
    @Qualifier("empServiceImpl")
    private EmpService empService;

    //删除员工
    @RequestMapping("/deleteEmpInfo/{empId}")
    public String deleteEmpInfo(@PathVariable("empId") int id){
        String msg = "";
        System.out.println("=============");
        boolean b = empService.deleteEmpInfo(id);
        if (b!=true){
            msg = "delete error!";
            return "redirect:/dept/alldept";
        }
        return "redirect:/dept/alldept";
    }

    //跳转到员工更新界面 /{empId}从前端拿到的数据

    @RequestMapping("/toUpdateEmoInfo/{empId}")
    public String toUpdateEmoInfo(Model model,@PathVariable("empId") int id){

        Emp emp = empService.queryEmpId(id);

//        //修改时间格式
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
//        Date empBirthday = emp.getEmpBirthday();
//        String bir = sdf.format(empBirthday);

        model.addAttribute("Qemp",emp);
//        model.addAttribute("Qbir",bir);
        return "emp/updateEmp";

    }
    //更新员工信息
    @RequestMapping("/updateEmoInfo")
    public String updateEmoInfo(Emp emp){
        String msg = "";
        boolean b = empService.updateEmp(emp);
        if (b==false){
            msg = "update error";
        }
        return "redirect:/dept/alldept";
    }

    //跳转到员工具体界面
    @RequestMapping("/toEmpInfo/{empId}")
    public String toEmpInfo(Model model,@PathVariable("empId") int id){
        System.out.println("=====");
        Emp emp = empService.queryEmpId(id);
        model.addAttribute("Qemp",emp);
        return "emp/empInfo";
    }

    //跳转到新增界面
    @RequestMapping("/toAddEmp")
    public String toAddEmp(){

        return "emp/addEmp";
    }


    @RequestMapping("/addEmpInfo")
    public String addEmp(Emp emp,Model model) {

        String msg = "";
        boolean b = empService.addEmp(emp);

        if (b) {
            return "redirect:/dept/alldept";
        } else {
            msg = "error";
            model.addAttribute("msg", msg);
            return "redirect:/emp/toAddEmp";
        }


    }
}
